package com.burgeon.backend.EnterpriseWeChat.rest.integral;

import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetIsBJ extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public GetIsBJ(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public HashMap getData(String code,String vipId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(code,vipId), url);
//        System.err.println(str);
        String code1 = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code1)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Vip> vipList = new ArrayList<>();

        if(jsonArray.length()>0){
           HashMap map = new HashMap();
           map.put("feedBack",jsonArray.getJSONArray(0).getString(0));
           map.put("tarGetDate",jsonArray.getJSONArray(0).getString(1));
            map.put("BILLDATE",jsonArray.getJSONArray(0).getString(2));
           return map;
        }
//        System.err.println(vipList.size());
        return null;
    }

    private MultiValueMap Data(String code,String vipId) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段
        colums.add("'FEEDBACK'");//邀约反馈
        colums.add("'TARGET_DATE'");//预约到店时间
        colums.add("'BILLDATE'");

        params.put("columns", colums);
        params.put("table", "C_VIP_APPRVISIT");//表名

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();
        expr1.put("column", "'M_PRODUCT_ID;NAME'");//款号
        String a = "'"+code+"'";
        expr1.put("condition", a);

        expr2.put("column", "'BILLTYPE'");//回访类型,活动邀约3
        String b = "'="+6+"'";
        expr2.put("condition", b);

        HashMap expr3 = new HashMap();
        expr3.put("combine", "and");
        expr3.put("expr1", expr1);
        expr3.put("expr2", expr2);

        HashMap expr4 = new HashMap();
        expr4.put("column", "'C_VIP_ID'");//回访类型,活动邀约3
        String c = "'"+vipId+"'";
        expr4.put("condition", c);

        paramss.put("combine", "and");
        paramss.put("expr1", expr3);
        paramss.put("expr2", expr4);

        params.put("params", paramss);
//        System.err.println(params);

        return DATA("Query", params, account, password);
    }


    public static void main(String[] args) {
        GetIsBJ g = new GetIsBJ("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(g.getData("ME183309Q281","1687"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
